import $ from 'jQuery';
(function () {
    let $vue = null;
    $vue = new Vue({
        el: '.create-account-hook',
        data: {
            but:'1',//显示第几个灯盏设置
            light:{
                name: '',
                unify:false,//true为统一设置
                // 按灯盏设置照明策略
            lightmethod: {
                lightmethod1: {
                    'type': 'sunset',// sunset按经纬度设置 timeset按时间节点设置
                    'sunsetLum': '',//按经纬度设置亮度
                    'startDelay': '',//开灯延迟时间
                    'endDelay': '',//关灯延迟时间
                    'timesetLum':'',//按时间设置亮度
                    'startTime': '',//按时间节点设置 开灯时间
                    'endTime': '',//按时间节点设置 关灯时间
                    'breakpointList': [],//按时间节点设置转换点
                    'breakpointSunList': [],// 按经纬度设置转换点
                },
                lightmethod2: {
                    'type': 'sunset',
                    'sunsetLum': '',
                    'startDelay': '',
                    'endDelay': '',
                    'timesetLum':'',
                    'startTime': '',
                    'endTime': '',
                    'breakpointList': [],
                    'breakpointSunList': [],
                },
                lightmethod3: {
                    'type': 'sunset',
                    'sunsetLum':'',
                    'startDelay': '',
                    'endDelay': '',
                    'timesetLum':'',
                    'startTime': '',
                    'endTime': '',
                    'breakpointList': [],
                    'breakpointSunList': [],
                },
                lightmethod4: {
                    'type': 'sunset',
                    'sunsetLum': '',
                    'startDelay': '',
                    'endDelay': '',
                    'timesetLum': '',
                    'startTime': '',
                    'endTime': '',
                    'breakpointList': [],
                    'breakpointSunList': [],
                },
            },
            //统一设置照明策略
            lightmethodUnify:{
                'type':'sunset',// sunset按经纬度设置 timeset按时间节点设置
                'sunsetLum': '',//按经纬度设置亮度
                'startDelay': '',//开灯点比日出延迟时间
                'endDelay': '',//关灯点比日出延迟时间
                'timesetLum': '',//按时间设置亮度
                'startTime': '',//按时间节点设置 开灯时间
                'endTime': '',//按时间节点设置 关灯时间
                'breakpointList': [],//按时间节点设置转换点
                'breakpointSunList': [],// 按经纬度设置转换点
            },
            }
        },
        methods: {
            goBack: function () {
                M.Page.emitPrePage();
            },
            addBreakpoint:function(){//新增转换点
                layer.open({
                    type: 1,
                    title: ['新增转换点'],
                    area: ['400px', '350px'],
                    content: $('#Newbreakpoint'),
                    btn: ['确定', '取消'],
                    yes: function(index, layero){
                    //按钮【按钮一】的回调
                        var bpName = $('#bpName').val();
                        var bpTime = $('#bpTime').val();
                        var bpLum = $('#bpLum').val();

                        if($vue.light.unify==true){
                            if($vue.light.lightmethodUnify.type=='timeset'){
                                $vue.light.lightmethodUnify.breakpointList.push({
                                    pointName:bpName,//转换点名称
                                    changeTime:bpTime,//转换点时间
                                    brightness:bpLum,//转换点亮度
                                });
                            }else{
                                $vue.light.lightmethodUnify.breakpointSunList.push({
                                    pointName:bpName,//转换点名称
                                    changeTime:bpTime,//转换点时间
                                    brightness:bpLum,//转换点亮度
                                });
                            }

                        }else{
                            if($vue.but==1){
                                if($vue.light.lightmethod.lightmethod1.type=='timeset'){
                                    $vue.light.lightmethod.lightmethod1.breakpointList.push({
                                        pointName:bpName,//转换点名称
                                        changeTime:bpTime,//转换点时间
                                        brightness:bpLum,//转换点亮度
                                    });
                                }else{
                                    $vue.light.lightmethod.lightmethod1.breakpointSunList.push({
                                        pointName:bpName,//转换点名称
                                        changeTime:bpTime,//转换点时间
                                        brightness:bpLum,//转换点亮度
                                    });
                                }
                            }else if($vue.but==2){
                                if($vue.light.lightmethod.lightmethod2.type=='timeset'){
                                    $vue.light.lightmethod.lightmethod2.breakpointList.push({
                                        pointName:bpName,//转换点名称
                                        changeTime:bpTime,//转换点时间
                                        brightness:bpLum,//转换点亮度
                                    });
                                }else{
                                    $vue.light.lightmethod.lightmethod2.breakpointSunList.push({
                                        pointName:bpName,//转换点名称
                                        changeTime:bpTime,//转换点时间
                                        brightness:bpLum,//转换点亮度
                                    });
                                }

                            }else if($vue.but==3){
                                if($vue.light.lightmethod.lightmethod3.type=='timeset'){
                                    $vue.light.lightmethod.lightmethod3.breakpointList.push({
                                        pointName:bpName,//转换点名称
                                        changeTime:bpTime,//转换点时间
                                        brightness:bpLum,//转换点亮度
                                    });
                                }else{
                                    $vue.light.lightmethod.lightmethod3.breakpointSunList.push({
                                        pointName:bpName,//转换点名称
                                        changeTime:bpTime,//转换点时间
                                        brightness:bpLum,//转换点亮度
                                    });
                                }
                            }else{
                                if($vue.light.lightmethod.lightmethod4.type=='timeset'){
                                    $vue.light.lightmethod.lightmethod4.breakpointList.push({
                                        pointName:bpName,//转换点名称
                                        changeTime:bpTime,//转换点时间
                                        brightness:bpLum,//转换点亮度
                                    });
                                }else{
                                    $vue.light.lightmethod.lightmethod4.breakpointSunList.push({
                                        pointName:bpName,//转换点名称
                                        changeTime:bpTime,//转换点时间
                                        brightness:bpLum,//转换点亮度
                                    });
                                }
                            }
                        };
                       $('#bpName').val('');
                       $('#bpTime').val('');
                       $('#bpLum').val('');
                        layer.closeAll();
                },
                })
            },
            del:function(index){
                // $vue.lightmethod.breakpointList.splice(index, 1);
                if($vue.light.unify==true){
                    if($vue.light.lightmethodUnify.type=='timeset'){
                        $vue.light.lightmethodUnify.breakpointList.splice(index, 1);
                    }else{
                        $vue.light.lightmethodUnify.breakpointSunList.splice(index, 1);
                    }
                }else{
                    if($vue.but==1){
                        if($vue.light.lightmethod.lightmethod1.type=='timeset'){
                            $vue.light.lightmethod.lightmethod1.breakpointList.splice(index, 1);
                        }else{
                            $vue.light.lightmethod.lightmethod1.breakpointSunList.splice(index, 1);
                        }
                    }else if($vue.but==2){
                        if($vue.light.lightmethod.lightmethod2.type=='timeset'){
                            $vue.light.lightmethod.lightmethod2.breakpointList.splice(index, 1);
                        }else{
                            $vue.light.lightmethod.lightmethod2.breakpointSunList.splice(index, 1);
                        }
                    }else if($vue.but==3){
                        if($vue.light.lightmethod.lightmethod3.type=='timeset'){
                            $vue.light.lightmethod.lightmethod3.breakpointList.splice(index, 1);
                        }else{
                            $vue.light.lightmethod.lightmethod3.breakpointSunList.splice(index, 1);
                        }
                    }else{
                        if($vue.light.lightmethod.lightmethod4.type=='timeset'){
                            $vue.light.lightmethod.lightmethod4.breakpointList.splice(index, 1);
                        }else{
                            $vue.light.lightmethod.lightmethod4.breakpointSunList.splice(index, 1);
                        }
                    }
                };
            }
        },
        mounted: function () {
            this.$nextTick(() => {
                $('.create-form-hook').validate({
                    debug: true,
                    submitHandler: function (form) {
                        // if($.trim($vue.$data.account.userName) == ""){
                        //     layer.alert('用户姓名不能为空');
                        //     return false;
                        // }
                        IOT.showOverlay('保存中，请稍等...');
                        // let params = $vue.$data.light;
                        // console.log($vue.light);
                        var params={
                            "list": [
                                {
                                    "closeBrightness": 0,
                                    "closeDelay": "",
                                    "closeTime": "日出时间",
                                    "lampSeri": "1",
                                    "lampStrategyId": "",
                                    "openBrightness":"",
                                    "openDelay":"",
                                    "openTime": "日落时间",
                                    "pointList": [],
                                    "strategyId": "",
                                    "strategyType": "0"//策略类型 0按照经纬度 1按照时间节点
                                },
                                {
                                    "closeBrightness": 0,
                                    "closeDelay": "",
                                    "closeTime": "日出时间",
                                    "lampSeri": "2",
                                    "lampStrategyId": "",
                                    "openBrightness":"",
                                    "openDelay":"",
                                    "openTime": "日落时间",
                                    "pointList": [],
                                    "strategyId": "",
                                    "strategyType": "0"//策略类型 0按照经纬度 1按照时间节点
                                },
                                {
                                    "closeBrightness": 0,
                                    "closeDelay": "",
                                    "closeTime": "日出时间",
                                    "lampSeri": "3",
                                    "lampStrategyId": "",
                                    "openBrightness":"",
                                    "openDelay":"",
                                    "openTime": "日落时间",
                                    "pointList": [],
                                    "strategyId": "",
                                    "strategyType": "0"//策略类型 0按照经纬度 1按照时间节点
                                },
                                {
                                    "closeBrightness": 0,//关灯亮度
                                    "closeDelay": "",//关灯延迟        endDelay
                                    "closeTime": "日出时间",//关灯时间   endtTime
                                    "lampSeri": "4",//灯盏序号
                                    "lampStrategyId": "",//灯盏策略Id
                                    "openBrightness": "",//开灯亮度  sunsetLum/timesetLum
                                    "openDelay": "",//开灯延迟           startDelay
                                    "openTime": "日落时间",//开灯时间    startTime
                                    "pointList": [],//灯盏转换列表     breakpointList/breakpointSunList
                                    "strategyId": "",//策略Id
                                    "strategyType": "0"//策略类型 0按照经纬度 1按照时间节点
                                }
                            ],
                            "strategyFlag": "1",//是否默认策略 0：默认 1：非默认
                            "strategyId": "",
                            "strategyName":$vue.light.name ,//策略名称
                            "unify":0,//1为统一设置，默认为0分开设置
                        };

                        if($vue.light.unify==true){
                            params.unify=1;
                            if($vue.light.lightmethodUnify.type=='sunset'){//按经纬度统一设置
                                params.list[0].closeDelay=params.list[1].closeDelay=params.list[2].closeDelay=params.list[3].closeDelay=$vue.light.lightmethodUnify.endDelay;//关灯延迟
                                params.list[0].openBrightness=params.list[1].openBrightness=params.list[2].openBrightness=params.list[3].openBrightness=$vue.light.lightmethodUnify.sunsetLum;//开灯亮度
                                params.list[0].openDelay=$vue.light.lightmethodUnify.startDelay;//开灯延迟
                                params.list[0].pointList=params.list[1].pointList=params.list[2].pointList=params.list[3].pointList=$vue.light.lightmethodUnify.breakpointSunList;//按经纬度设置转换点
                            }else{//按时间节点统一设置
                                params.list[0].closeTime=params.list[1].closeTime=params.list[2].closeTime=params.list[3].closeTime=$vue.light.lightmethodUnify.endTime;//关灯时间
                                params.list[0].openBrightness=params.list[1].openBrightness=params.list[2].openBrightness=params.list[3].openBrightness=$vue.light.lightmethodUnify.timesetLum;//开灯亮度
                                params.list[0].openTime=params.list[1].openTime=params.list[2].openTime=params.list[3].openTime=$vue.light.lightmethodUnify.startTime;//开灯时间
                                params.list[0].pointList=params.list[1].pointList=params.list[2].pointList=params.list[3].pointList=$vue.light.lightmethodUnify.breakpointList;//按时间节点设置转换点
                                params.list[0].strategyType=params.list[1].strategyType=params.list[2].strategyType=params.list[3].strategyType="1";
                            }
                        }else{
                            let $vl=$vue.light.lightmethod;
                            if($vl.lightmethod1.type=="sunset"){
                                params.list[0].closeDelay=$vl.lightmethod1.endDelay;//关灯延迟
                                params.list[0].openBrightness=$vl.lightmethod1.sunsetLum;//开灯亮度
                                params.list[0].openDelay=$vl.lightmethod1.startDelay;//开灯延迟
                                params.list[0].pointList=$vl.lightmethod1.breakpointSunList;//按经纬度设置转换点
                            }else{
                                params.list[0].closeTime=$vl.lightmethod1.endTime;//关灯时间
                                params.list[0].openBrightness=$vl.lightmethod1.timesetLum;//开灯亮度
                                params.list[0].openTime=$vl.lightmethod1.startTime;//开灯时间
                                params.list[0].pointList=$vl.lightmethod1.breakpointList;//按时间节点设置转换点
                                params.list[0].strategyType="1";
                            };

                            if($vl.lightmethod2.type=="sunset"){
                                params.list[1].closeDelay=$vl.lightmethod2.endDelay;
                                params.list[1].openBrightness=$vl.lightmethod2.sunsetLum;
                                params.list[1].openDelay=$vl.lightmethod2.startDelay;
                                params.list[1].pointList=$vl.lightmethod2.breakpointSunList;
                            }else{
                                params.list[1].closeTime=$vl.lightmethod2.endTime;
                                params.list[1].openBrightness=$vl.lightmethod2.timesetLum;
                                params.list[1].openTime=$vl.lightmethod2.startTime;
                                params.list[1].pointList=$vl.lightmethod2.breakpointList;
                                params.list[1].strategyType="1";
                            };

                            if($vl.lightmethod3.type=="sunset"){
                                params.list[2].closeDelay=$vl.lightmethod3.endDelay;
                                params.list[2].openBrightness=$vl.lightmethod3.sunsetLum;
                                params.list[2].openDelay=$vl.lightmethod3.startDelay;
                                params.list[2].pointList=$vl.lightmethod3.breakpointSunList;
                            }else{
                                params.list[2].closeTime=$vl.lightmethod3.endTime;
                                params.list[2].openBrightness=$vl.lightmethod3.timesetLum;
                                params.list[2].openTime=$vl.lightmethod3.startTime;
                                params.list[2].pointList=$vl.lightmethod3.breakpointList;
                                params.list[2].strategyType="1";
                            };

                            if($vl.lightmethod4.type=="sunset"){
                                params.list[3].closeDelay=$vl.lightmethod4.endDelay;
                                params.list[3].openBrightness=$vl.lightmethod4.sunsetLum;
                                params.list[3].openDelay=$vl.lightmethod4.startDelay;
                                params.list[3].pointList=$vl.lightmethod4.breakpointSunList;
                            }else{
                                params.list[3].closeTime=$vl.lightmethod4.endTime;
                                params.list[3].openBrightness=$vl.lightmethod4.timesetLum;
                                params.list[3].openTime=$vl.lightmethod4.startTime;
                                params.list[3].pointList=$vl.lightmethod4.breakpointList;
                                params.list[3].strategyType="1";
                            };
                        }
                        // console.log(params);
                        IOT.getServerData(URI.OPERATE.LIGHTMETHOD.CREATE.SAVE, params, function (ret) {
                            console.log(params);
                            IOT.hideOverlay();
                            if (ret && ret.code === 200) {
                                IOT.tips('保存成功！', 'success');
                                M.Table.refresh.all();
                                M.Page.emitPrePage();
                            } else {
                                IOT.tips(ret.msg || '操作失败，请重试！', 'error', 1000, null);
                            }
                        });
                        return false;
                    }
                });
            });
        }
    });
})();